class FinancesController < ApplicationController
  layout "m"

    before_filter :login_required
    before_filter :group_setup

    def group_setup
      if params[:group_id]
        @group_id = params[:group_id]
      elsif current_user.groups.size == 1
        @group_id = current_user.groups[0].id
      else
        @group_id =1
        #flash[:notice] = "Your group was not selected or does not exist"
        #redirect_to :controller => 'groups', :action => :index
      end
    end

    def edit
      @line_item = LineItem.find(params[:id])
      @fundid = @line_item.fund_id
      @line_item_entries = LineItemEntry.find(:all, :conditions =>["line_item_id = ?", params[:id]], :order => "created_at ASC")
      @funds = Fund.find(:all,:conditions=>["status!=0"])
      @users = User.find(:all)
      @other_items = LineItem.find(:all, :conditions => ["fund_id = ? AND id != ?", @fundid, @line_item.id])
      return unless request.post?
      entry = LineItemEntry.new(params[:line_item_entry])
      entry.created_by = current_user
      @line_item.line_item_entries << entry
      LineItem.update(params[:id],params[:line_item])
      if @line_item.first_entry.status == "Closed"
        @line_item.status = 0
      else
        @line_item.status = 1
      end
      @line_item.shead = entry.name
      @line_item.sbody = entry.note
      @line_item.smeta = entry.note
      if @line_item.save
        redirect_to :action => 'edit'
      else
          flash[:error] = @line_item.line_item_entries.errors.full_messages
      end
    end

    def edit_fund
      @fund = Fund.find(params[:id])
      @fund_entries = FundEntry.find(:all, :conditions =>["fund_id = ?", params[:id]], :order => "created_at ASC")
      @line_items = LineItem.find(:all, :conditions =>["fund_id =?", params[:id]])
      return unless request.post?
      entry = FundEntry.new(params[:fund_entry])
      entry.created_by_id = current_user.id
      @fund.fund_entries << entry
      if @fund.first_entry.status == "Closed"
        @fund.status = 0
        for item in @line_items
          item.status = 0
          item.save
        end
      else
        @fund.status = 1
      end
      @fund.shead = entry.name
      @fund.sbody = entry.note
      @fund.smeta = entry.note
      if @fund.save
        redirect_to :action => :edit_fund
      else
          flash[:error] = entry.errors.full_messages
      end
    end

    def index

    end
    def list
      @lineitems = LineItem.find(:all, :conditions=>["status!=0"])
      @funds = Fund.find(:all, :conditions=>["status!=0"])
      render :partial => 'list'
    end
    def new
      @users = User.find(:all,:conditions=>["status != 0"])
      if Fund.find(:all,:conditions=>["status!=0"]).length() > 0
      else
        redirect_to :action => :index
        flash[:error] = "You must first create a project before adding action items"
      end
      @funds = Fund.find(:all,:conditions=>["status!=0"])
      @users = User.find(:all)
      # save action item and entry
      @line_item = LineItem.new(params[:line_item])    
      return unless request.post? 
      entry = LineItemEntry.new(params[:line_item_entry])
      entry.created_by = current_user
      @line_item.status = 1
      @line_item.shead = entry.name
      @line_item.sbody = entry.note
      @line_item.smeta = entry.note
      @line_item.line_item_entries << entry
      if @line_item.save
        redirect_to :action => 'index'
      else
        flash[:error] = entry.errors.full_messages
      end
    end

    def new_fund
      # save project and entry
      @fund = Fund.new(params[:fund])
      return unless request.post?
      entry = FundEntry.new(params[:fund_entry])
      entry.created_by_id = current_user.id
      @fund.status = 1
      @fund.shead = entry.name
      @fund.sbody = entry.note
      @fund.smeta = entry.note
      @fund.fund_entries << entry
      if @fund.save
        redirect_to :action => 'index'
      else
          flash[:error] = entry.errors.full_messages
      end
    end
end